package com.iwunu.gateway.service.impl;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iwunu.gateway.entity.FLockUser;
import com.iwunu.gateway.repository.FaceUserRepository;
import com.iwunu.gateway.service.FlockUserService;
import com.iwunu.gateway.service.JpaService;
import com.iwunu.gateway.vo.UserUpdateVo;
import com.iwunu.gateway.vo.UserVo;
import com.iwunu.iot.common.util.DateUtil;

@Service
public class FlockUserServiceImpl implements FlockUserService{
	  @Autowired
	    private FaceUserRepository faceUserRepository;
	    @Autowired
	    JpaService jpaService;
		public  FLockUser getFlackUserByUserId(Long userId) {
			FLockUser u = faceUserRepository.getFlackUserByUserId(userId);
			return u;
		}
		
		/**
		 * *保存用户
		 * @param userVo
		 * @return
		 */
		public  FLockUser save(UserVo userVo) {
			FLockUser flUser = new FLockUser(userVo.getPhone());
			flUser.setAvatar(userVo.getAvatar());
			flUser.setCreateTime(DateUtil.nowDate());
			flUser.setOpenId(userVo.getOpenId());
			flUser.setNickName(userVo.getNickName());
			flUser.setUuid(UUID.randomUUID().toString());
			FLockUser result = faceUserRepository.save(flUser);
			return result;
		}

		@Override
		public FLockUser update( FLockUser flUser,UserUpdateVo userVo) {
			if(StringUtils.isNoneEmpty(userVo.getAvatar())) {
				flUser.setAvatar(userVo.getAvatar());
			}
			if(StringUtils.isNoneEmpty(userVo.getOpenId())) {
				flUser.setOpenId(userVo.getOpenId());
			}
			if(StringUtils.isNoneEmpty(userVo.getNickName())) {
				flUser.setNickName(userVo.getNickName());
			}
//			if(userVo.getType()!=null) {
//				flUser.setType(userVo.getType());
//			}
			flUser.setLastLoginTime(DateUtil.nowDateTime());
			FLockUser result = faceUserRepository.save(flUser);
			return result;
		}

		@Override
		public void del(String userId) {
			faceUserRepository.deleteById(Long.valueOf(userId));
		}

	    @Override
	    public FLockUser findByParams(Map<String, Object> params) {
	        try{
	            StringBuffer sql =  new StringBuffer("select * from t_flock_user  where 1=1 ") ;
	            for(String key:params.keySet()){
	                sql.append(" and ").append(key).append("='").append(params.get(key)).append("'");
	            }
	            List<FLockUser> list = jpaService.findListByNativeSql(sql.toString(),FLockUser.class);
	            return list.get(0);
	        }catch (Exception e){
	            return null;
	        }
	    }
public static void main(String[] args) {
	String s = null;
	String str = "";
	
	System.out.println(StringUtils.isNoneEmpty("11"));
	System.out.println(StringUtils.isNoneEmpty(s));
}
}
